﻿using System;
using System.Windows.Forms;

using System.IO.Ports;

namespace Titan.FIS.LmsBalanceCollect
{
    public partial class frmSet : Form
    {
        public frmSet()
        {
            InitializeComponent();
        }

        private void frmSet_Load(object sender, EventArgs e)
        {
            cbComNo.Items.Clear();
            foreach (var s in SerialPort.GetPortNames())
                cbComNo.Items.Add(s);

            cbBaNo.SelectedIndex = 0;
            cbBaudRate.SelectedIndex = 6;
            cbDataBits.SelectedIndex = 3;
            cbStopBits.SelectedIndex = 0;
            cbParity.SelectedIndex = 0;
        }

        /// <summary> 保存用户设置 </summary>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbBaNo.SelectedIndex == 0)
                {
                    Titan.FIS.LmsBalanceCollect.Properties.Settings.Default.BaudRate1 = int.Parse(cbBaudRate.Text);
                    Titan.FIS.LmsBalanceCollect.Properties.Settings.Default.DataBits1 = int.Parse(cbDataBits.Text);
                    Titan.FIS.LmsBalanceCollect.Properties.Settings.Default.Parity1 = (Parity)System.Enum.Parse(typeof(Parity), cbParity.Text);
                    Titan.FIS.LmsBalanceCollect.Properties.Settings.Default.StopBits1 = (StopBits)System.Enum.Parse(typeof(StopBits), cbStopBits.Text);
                    Titan.FIS.LmsBalanceCollect.Properties.Settings.Default.PortName1 = cbComNo.Text;
                }
                else if (cbBaNo.SelectedIndex == 1)
                {
                    Titan.FIS.LmsBalanceCollect.Properties.Settings.Default.BaudRate2 = int.Parse(cbBaudRate.Text);
                    Titan.FIS.LmsBalanceCollect.Properties.Settings.Default.DataBits2 = int.Parse(cbDataBits.Text);
                    Titan.FIS.LmsBalanceCollect.Properties.Settings.Default.Parity2 = (Parity)System.Enum.Parse(typeof(Parity), cbParity.Text);
                    Titan.FIS.LmsBalanceCollect.Properties.Settings.Default.StopBits2 = (StopBits)System.Enum.Parse(typeof(StopBits), cbStopBits.Text);
                    Titan.FIS.LmsBalanceCollect.Properties.Settings.Default.PortName2 = cbComNo.Text;
                }

                Titan.FIS.LmsBalanceCollect.Properties.Settings.Default.Save();
                MessageBox.Show("保存串口参数成，谢谢！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存串口参数异常，请重试！" + ex.Message.ToString());
            }
        }


    }
}
